package com.gzq.prototype;

import java.io.*;

public class Human implements Cloneable, Serializable {

    private int age;
    private Gun gun;

    public void setAge(int age) {
        this.age = age;
    }

    public Gun getGun() {
        return gun;
    }

    public void setGun(Gun gun) {
        this.gun = gun;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        Human clone = (Human) super.clone();
//        clone.gun = (Gun) clone.gun.clone();
        return clone;
//        return super.clone();
    }

    @Override
    public String toString() {
        return "Human{" +
                "age=" + age +
                ", gun=" + gun +
                '}';
    }

    public Human deepClone() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(this);

        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bais);
        Human o = (Human) ois.readObject();
        return o;
    }
}
